package com.talent.jiwen.ui.widgets;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.http.result.Member;
import com.talent.jiwen.http.result.OrderResult;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class CourseInfoView extends LinearLayout {

    private TextView contentTv;
    private RecyclerView imgRecyclerView;
    private RecyclerView teamRecyclerView;
    private LinearLayout memberLayout;

    public CourseInfoView(Context context) {
        super(context);
        init();
    }

    public CourseInfoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CourseInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        inflate(getContext() , R.layout.view_course_info , this);

        contentTv = (TextView)findViewById(R.id.contentTv);
        imgRecyclerView = (RecyclerView)findViewById(R.id.imgRecyclerView);
        teamRecyclerView = (RecyclerView)findViewById(R.id.teamRecyclerView);
        memberLayout = (LinearLayout)findViewById(R.id.memberLayout);

    }

    public void bindData(final CourseInfoData data){

        //文字内容
        if (!Validators.isEmpty(data.getContent())){
            contentTv.setVisibility(VISIBLE);
            contentTv.setText(data.getContent());
        }else{
            contentTv.setVisibility(GONE);
        }

        //图片内容
        if (!Validators.isEmpty(data.getImgList())){
            imgRecyclerView.setVisibility(VISIBLE);
            LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
            imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            imgRecyclerView.setLayoutManager(imgLayoutManager);
            imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_add_problem_img , data.getImgList()){
                @Override
                protected void convert(ViewHolder holder, String s, final int position) {
                    ImageUtil.loadCorner4Image(getContext() , s , (ImageView) holder.getView(R.id.imgIv));

                    holder.getView(R.id.imgIv).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            ImageUtil.picturePreviewWithUrl((Activity)getContext(), position  , data.getImgList());
                        }
                    });
                }
            });
        }else{
            imgRecyclerView.setVisibility(GONE);
        }

        //成员列表
        if (!Validators.isEmpty(data.getMemberList())){
            memberLayout.setVisibility(VISIBLE);

            LinearLayoutManager teamLayoutManager = new LinearLayoutManager(getContext());
            teamLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            teamRecyclerView.setLayoutManager(teamLayoutManager);
            teamRecyclerView.setAdapter(new CommonAdapter<Member>(getContext() , R.layout.item_course_teammate , data.getMemberList()){
                @Override
                protected void convert(ViewHolder holder, Member member, int position) {
                    ImageUtil.loadCircleImage(getContext() , member.getStudentHeadImage() , (ImageView)holder.getView(R.id.memberHeadIv));

//                    TextView memberNameTv = (TextView)holder.getView(R.id.memberNameTv);
//                    memberNameTv.setText(member.getStudentName());
                }
            });
        }else{
            memberLayout.setVisibility(GONE);
        }

    }

    public static class CourseInfoData{
        private String content;
        private List<String> imgList;
        private List<Member> memberList;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public List<String> getImgList() {
            return imgList;
        }

        public void setImgList(List<String> imgList) {
            this.imgList = imgList;
        }

        public List<Member> getMemberList() {
            return memberList;
        }

        public void setMemberList(List<Member> memberList) {
            this.memberList = memberList;
        }
    }
}
